﻿using System;
using System.Collections.Generic;
using System.Linq;
using PeopleException;
using Interfaces;
using PeopleEntity;

namespace PeopleDal
{
    public class NewsDal : INews
    {
        private readonly PeopleContext _context;
        public NewsDal()
        {
           // _context = new PeopleContext(Helper.GetConnetction());
            _context=new PeopleContext("PeopleContext");
        }
        public bool Create(News t)
        {
            bool result = false;
            try
            {
                _context.News.Add(t);
                result = _context.SaveChanges() > 0;
            }
            catch (Exception e)
            {
                var ex = string.Format("Exception in work with base: {0}", e.Message);
                throw new DalException(ex);
            }
            return result;
        }

        public News Read(Guid id)
        {
            News news = null;
            try
            {
                news=_context.News.FirstOrDefault(s => s.Id == id);
            }
            catch (Exception e)
            {
                var ex = string.Format("Exception in work with base: {0}", e.Message);
                throw new DalException(ex);
            }
            return news;
        }

        public List<News> ReadAll()
        {
            List<News> lst = null;
            try
            {
                lst= _context.News.ToList();
            }
            catch (Exception e)
            {
                var ex = string.Format("Exception in work with base: {0}", e.Message);
                throw new DalException(ex);
            }
            return lst;
        }
    }
}
